<?php
	class html {
		// Setting html server IP & MySQL server ...
		// const WebIP = 'http://10.10.5.184';		// Test IP
		// const WebIP = 'http://10.10.5.109';		// Test IP
		const WebIP = 'http://localhost:881';	
				
		
		// Setting MySQL Account ...
		const sql_login_host = 'localhost';		// MySQL Host
		const sql_login_user = 'root';			// MySql UserName
		//const sql_login_pass = 'admin@201503';	// MySql Password
		const sql_login_pass = 'root';
		// Setting FTP update username / password ...
		const FtpIP = '10.10.30.65';
		const ftp_upload_user = 'webdl';		// FTP Web update user name
		const ftp_upload_pass = '!web201512';	// FTP Web update password
	}
	
	class member {
		var $admin_member;
		var $name_array;
		
		// admin 成员定义 ...
		// ############################################################################
		// PROCEDURE NAME - getMembers
		// -------------------------------------------------------------------------
		// DESCRIPTION: To get admin members ...
		// EXAMPLE : $admin_member = new member();
		//           $members = "$admin_member->getMembers()";
		// RETURN: $admin_member
		// ############################################################################ 
		public function getMembers() {
			$this->admin_member[] = 'admin2015';
			$this->admin_member[] = 'max_yin';
			$this->admin_member[] = 'binz_zhang';
			$this->admin_member[] = 'weibiao_yuan';
			$this->admin_member[] = 'shuihua_chen';
			$this->admin_member[] = '970075';		// 'js_huang';
			return $this->admin_member;
		}
		
		// admin 成员定义 ...
		// ############################################################################
		// PROCEDURE NAME - SpecialName_Mapping($number)
		// -------------------------------------------------------------------------
		// DESCRIPTION: To mapping Special members ...
		// EXAMPLE : 
		// RETURN: $this->return_name
		// ############################################################################ 
		public function SpecialName_Mapping($number) {
			$this->name_array['920456'] 	= 'alan_lee';
			$this->name_array['940306'] 	= 'chchen';
			$this->name_array['970075'] 	= 'js_huang';
			$this->name_array['920860'] 	= 'elvis_huang';
			$this->name_array['A1000300'] 	= 'peter588_su';
			$this->name_array['A1000308'] 	= 'paine_peng';
			$this->name_array['A1010157'] 	= 'gary_yeh';
			$this->name_array['910508'] 	= 'junior_liu';
			$this->name_array['A1000063'] 	= 'jay_cheng';
			$this->name_array['A1000302'] 	= 'maxime_lu';
			$this->name_array['A1000333'] 	= 'mingway_lee';
			
			if ( $this->name_array[$number] == NULL ) {
				return $number;
			} else {
				return $this->name_array[$number];
			}
		}
	}
	
	// The PHP source code copy from internet...
	class xml {		
		private $xml;
		
		/*-------------------------------------
		*    将数组或对象转换成XML-DOM格式
		*-------------------------------------*/
		public function __make_xmk($data) {
			$this->xml = '';
			foreach ($data as $key => $value) {
				//如果$key是一个数字，前面加item
				(is_numeric($key)) ? $key='item'.$key : $key;
				$this->xml.="<{$key}>";
				//如果遍历的$value还是一个array,回调
				$this->xml.= ( is_array($value) || is_object($value)) ? $this->make_xmk($value) : $value;
				$this->xml.="</{$key}>";
			}
			return $this->xml;
		}
	}
	
	class Sql_Account {
		const sql_db      = 'web_account';		// MySql Account Database
		const sql_table   = 'account_member';	// MySql Account Table
		const table_name1 = 'username';
		const table_name2 = 'password';
		const table_name3 = 'create_date';
		const table_name4 = 'create_by';
	}
	
	class Sql_Download {
		const sql_db       = 'web_download';	// MySql Download Database
		const sql_table    = 'download_data2';	// MySql Download Table
		const table_name1  = 'webdl_pn';
		const table_name2  = 'webdl_wsid';
		const table_name3  = 'webdl_tpver';
		const table_name4  = 'webdl_work_order';
		const table_name5  = 'webdl_line';
		const table_name6  = 'webdl_panel';
		const table_name7  = 'webdl_ftpip';
		const table_name8  = 'webdl_create_date';
		const table_name9  = 'webdl_creator';
		const table_name10 = 'webdl_description';
		const table_name11 = 'webdl_count';
		const table_name12 = 'webdl_download_date';
	}
	
	class Stations {
		var $station_chk;
		
		// admin 成员定义 ...
		// ############################################################################
		// PROCEDURE NAME - Station_Exist($ChkStr)
		// -------------------------------------------------------------------------
		// DESCRIPTION: To get station then check exist or not ...
		// EXAMPLE : 
		// RETURN: 0/1
		// ############################################################################ 
		public function Station_Exist($ChkStr) {
			// $this->station_chk['SMT-PRINT']  = 1;
			// $this->station_chk['SMT-SPI'] 	 = 1;
			// $this->station_chk['SMT-GP'] 	 = 1;
			// $this->station_chk['SMT-AOI'] 	 = 1;
			// $this->station_chk['SMT-AXI'] 	 = 1;
			// $this->station_chk['SMT-RS'] 	 = 1;
			$this->station_chk['SMT'] 	 	= 1;
			$this->station_chk['P/T'] 		= 1;
			$this->station_chk['PT1'] 		= 1;
			$this->station_chk['PT2']		= 1;
			$this->station_chk['PT3'] 		= 1;
			$this->station_chk['PT4'] 		= 1;
			$this->station_chk['PT5'] 		= 1;
			$this->station_chk['PT6'] 		= 1;
			$this->station_chk['PDL-P'] 	= 1;
			$this->station_chk['B/I'] 		= 1;
			$this->station_chk['B/I1'] 		= 1;
			$this->station_chk['B/I1-A2'] 	= 1;
			$this->station_chk['B/I2'] 		= 1;
			$this->station_chk['B/I2-A2'] 	= 1;
			$this->station_chk['BI-A1'] 	= 1;
			$this->station_chk['BI-A2'] 	= 1;
			$this->station_chk['B/I-P'] 	= 1;
			$this->station_chk['BI1'] 		= 1;
			$this->station_chk['BI2'] 		= 1;
			$this->station_chk['BI1-A2'] 	= 1;
			$this->station_chk['BI2-A2'] 	= 1;
			$this->station_chk['DL'] 		= 1;
			$this->station_chk['F/T-B'] 	= 1;
			$this->station_chk['F/T-B-C1']	= 1;
			$this->station_chk['F/T-B1']	= 1;
			$this->station_chk['F/T-B2']	= 1;
			$this->station_chk['F/T-B-A2']	= 1;
			$this->station_chk['F/T'] 		= 1;
			$this->station_chk['F/T-A2'] 	= 1;
			$this->station_chk['F/T-BI2C']	= 1;
			$this->station_chk['F/T-C']		= 1;
			$this->station_chk['FT1'] 		= 1;
			$this->station_chk['FT2'] 		= 1;
			$this->station_chk['FT3'] 		= 1;
			$this->station_chk['FT4'] 		= 1;
			$this->station_chk['FT5'] 		= 1;
			$this->station_chk['FT6'] 		= 1;
			$this->station_chk['FT-A2'] 	= 1;
			$this->station_chk['FT-B']		= 1;
			$this->station_chk['FT-B-A2'] 	= 1;
			$this->station_chk['FDL-A2'] 	= 1;
			$this->station_chk['FT1-A2'] 	= 1;
			$this->station_chk['FT2-A2'] 	= 1;
			$this->station_chk['FT3-A2'] 	= 1;
			$this->station_chk['FT4-A2'] 	= 1;
			$this->station_chk['FT5-A2'] 	= 1;
			$this->station_chk['FT6-A2'] 	= 1;
			$this->station_chk['FT-A3'] 	= 1;
			$this->station_chk['FT1-A3'] 	= 1;
			$this->station_chk['FT2-A3'] 	= 1;
			$this->station_chk['FT3-A3'] 	= 1;
			$this->station_chk['FT4-A3'] 	= 1;
			$this->station_chk['FT5-A3'] 	= 1;
			$this->station_chk['FT6-A3'] 	= 1;
			$this->station_chk['FT-WT'] 	= 1;
			$this->station_chk['FDL-P']		= 1;
			$this->station_chk['FGI']		= 1;
			$this->station_chk['H/P']		= 1;
			$this->station_chk['H/P-P'] 	= 1;
			$this->station_chk['H/P-S'] 	= 1;
			
			$this->station_chk['P/T_SOP']		 = 1;			
			$this->station_chk['PT1_SOP'] 		 = 1;
			$this->station_chk['PT2_SOP']		 = 1;
			$this->station_chk['PT3_SOP'] 		 = 1;
			$this->station_chk['PT4_SOP'] 		 = 1;
			$this->station_chk['PT5_SOP'] 		 = 1;
			$this->station_chk['PT6_SOP'] 		 = 1;
			$this->station_chk['PDL-P_SOP'] 	 = 1;
			$this->station_chk['B/I_SOP'] 		 = 1;
			$this->station_chk['B/I1_SOP'] 		 = 1;
			$this->station_chk['B/I1-A2_SOP']	 = 1;
			$this->station_chk['B/I2_SOP'] 		 = 1;
			$this->station_chk['B/I2-A2_SOP'] 	 = 1;
			$this->station_chk['B/I-P_SOP'] 	 = 1;
			$this->station_chk['BI1_SOP'] 		 = 1;
			$this->station_chk['BI2_SOP'] 		 = 1;
			$this->station_chk['BI-A1_SOP'] 	 = 1;
			$this->station_chk['BI-A2_SOP'] 	 = 1;
			$this->station_chk['BI1-A2_SOP'] 	 = 1;
			$this->station_chk['BI2-A2_SOP'] 	 = 1;
			$this->station_chk['DL_SOP'] 		 = 1;
			$this->station_chk['F/T-B_SOP'] 	 = 1;
			$this->station_chk['F/T-B-C1_SOP']	 = 1;
			$this->station_chk['F/T-B1_SOP']	 = 1;
			$this->station_chk['F/T-B2_SOP']	 = 1;
			$this->station_chk['F/T-B-A2_SOP']	 = 1;
			$this->station_chk['F/T_SOP'] 		 = 1;
			$this->station_chk['F/T-A2_SOP']     = 1;
			$this->station_chk['F/T-BI2C_SOP']	 = 1;
			$this->station_chk['F/T-C_SOP']		 = 1;
			$this->station_chk['FT1_SOP'] 		 = 1;
			$this->station_chk['FT2_SOP'] 		 = 1;
			$this->station_chk['FT3_SOP'] 		 = 1;
			$this->station_chk['FT4_SOP'] 		 = 1;
			$this->station_chk['FT5_SOP'] 		 = 1;
			$this->station_chk['FT6_SOP'] 		 = 1;
			$this->station_chk['FT-A2_SOP'] 	 = 1;
			$this->station_chk['FT-B_SOP']		 = 1;
			$this->station_chk['FT-B-A2_SOP'] 	 = 1;
			$this->station_chk['FDL-A2_SOP'] 	 = 1;
			$this->station_chk['FT1-A2_SOP'] 	 = 1;
			$this->station_chk['FT2-A2_SOP'] 	 = 1;
			$this->station_chk['FT3-A2_SOP'] 	 = 1;
			$this->station_chk['FT4-A2_SOP'] 	 = 1;
			$this->station_chk['FT5-A2_SOP'] 	 = 1;
			$this->station_chk['FT6-A2_SOP'] 	 = 1;
			$this->station_chk['FT-A3_SOP'] 	 = 1;
			$this->station_chk['FT1-A3_SOP'] 	 = 1;
			$this->station_chk['FT2-A3_SOP'] 	 = 1;
			$this->station_chk['FT3-A3_SOP'] 	 = 1;
			$this->station_chk['FT4-A3_SOP'] 	 = 1;
			$this->station_chk['FT5-A3_SOP'] 	 = 1;
			$this->station_chk['FT6-A3_SOP'] 	 = 1;
			$this->station_chk['FT-WT_SOP'] 	 = 1;
			$this->station_chk['FDL-P_SOP']		 = 1;
			$this->station_chk['FGI_SOP']		 = 1;
			$this->station_chk['H/P_SOP']		 = 1;
			$this->station_chk['H/P-P_SOP'] 	 = 1;
			$this->station_chk['H/P-S_SOP'] 	 = 1;
			
			$this->station_chk['SCT'] 		 	 = 1;
			$this->station_chk['SCT1'] 		 	 = 1;
			$this->station_chk['SCT2'] 		 	 = 1;
			$this->station_chk['SCT3'] 		 	 = 1;
			$this->station_chk['SCT4'] 		 	 = 1;
			$this->station_chk['SCT5']  	 	 = 1;
			$this->station_chk['SCT6'] 		 	 = 1;
			$this->station_chk['SCT_SOP'] 		 = 1;
			$this->station_chk['SCT1_SOP'] 		 = 1;
			$this->station_chk['SCT2_SOP'] 		 = 1;
			$this->station_chk['SCT3_SOP'] 		 = 1;
			$this->station_chk['SCT4_SOP'] 		 = 1;
			$this->station_chk['SCT5_SOP']  	 = 1;
			$this->station_chk['SCT6_SOP'] 		 = 1;
			$this->station_chk['ICT'] 		 	 = 1;
			$this->station_chk['ICT1'] 		 	 = 1;
			$this->station_chk['ICT2'] 		 	 = 1;
			$this->station_chk['ICT3'] 		 	 = 1;
			$this->station_chk['ICT4'] 		 	 = 1;
			$this->station_chk['ICT5'] 		 	 = 1;
			$this->station_chk['ICT6'] 		 	 = 1;
			$this->station_chk['ICT_SOP'] 		 = 1;
			$this->station_chk['ICT1_SOP'] 		 = 1;
			$this->station_chk['ICT2_SOP'] 		 = 1;
			$this->station_chk['ICT3_SOP'] 		 = 1;
			$this->station_chk['ICT4_SOP'] 		 = 1;
			$this->station_chk['ICT5_SOP'] 		 = 1;
			$this->station_chk['ICT6_SOP'] 		 = 1;
			$this->station_chk['FLASH']		 	 = 1;
			$this->station_chk['ROUTER']	 	 = 1;
			$this->station_chk['WS-PROFILE'] 	 = 1;
			$this->station_chk['ESD']        	 = 1;
			$this->station_chk['PWC']        	 = 1;
			
			$this->station_chk['FLASH_SOP']		 = 1;
			$this->station_chk['ROUTER_SOP']	 = 1;
			$this->station_chk['WS-PROFILE_SOP'] = 1;
			$this->station_chk['ESD_SOP']        = 1;
			$this->station_chk['PWC_SOP']    	 = 1;
			$this->station_chk['DOC'] 		 	 = 1;
			
			if ( $this->station_chk[$ChkStr] == NULL ) {
				return 0;
			} else {
				return $this->station_chk[$ChkStr];
			}
		}
	}
	
	// ############################################################################
	// PROCEDURE NAME - account_create($username,$password,$date_time,$tmp_by)
	// -------------------------------------------------------------------------
	// DESCRIPTION: Create new account to MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function account_create($username,$password,$date_time,$tmp_by) {
		$db_select   = Sql_Account::sql_db;
		$db_table    = Sql_Account::sql_table;
		$table_name1 = Sql_Account::table_name1;	// 'username';
		$table_name2 = Sql_Account::table_name2;	// 'password';
		$table_name3 = Sql_Account::table_name3;	// 'create_date';
		$table_name4 = Sql_Account::table_name4;	// 'create_by';
		$conn        = '';
		$result      = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) {
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// INSERT INTO `web_account`.`account_member` (`username`, `password`, `date_time`, `tmp_by`) VALUES ('test4', 'test4', '2015-06-14 18:29:10', 'max_yin');
		$sql_cmd = "INSERT INTO `$db_select`.`$db_table`(`$table_name1`,`$table_name2`,`$table_name3`,`$table_name4`) VALUES('$username','$password','$date_time','$tmp_by')";
		$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成添加
		mysqli_close($conn);						// 关闭资料库
		if ( !$result ) return 0; else return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - account_update($username,$password,$date_time,$tmp_by)
	// -------------------------------------------------------------------------
	// DESCRIPTION: To update account data to MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function account_update($username,$password,$date_time,$tmp_by) {
		$db_select   = Sql_Account::sql_db;
		$db_table    = Sql_Account::sql_table;
		$table_name1 = Sql_Account::table_name1;	// 'username';
		$table_name2 = Sql_Account::table_name2;	// 'password';
		$table_name3 = Sql_Account::table_name3;	// 'create_date';
		$table_name4 = Sql_Account::table_name4;	// 'create_by';
		$conn        = '';
		$rows        = 0;
		$result      = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// UPDATE `account_member` SET `username`=[value-1],`password`=[value-2],`create_date`=[value-3],`create_by`=[value-4] WHERE `username` = 'max_yin'
		$sql_cmd = "UPDATE `$db_table` SET `$table_name1`='$username',`$table_name2`='$password',`$table_name3`='$date_time',`$table_name4`='$tmp_by' WHERE `$table_name1` = '$username'";
		$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成添加
		$rows    = mysqli_affected_rows($conn);		// 获取刚刚更新资料笔数
		mysqli_close($conn);						// 关闭资料库
		if ( $rows == 0 ) return 0;
		if ( !$result ) return 0; else return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - search_result($chk_username)
	// -------------------------------------------------------------------------
	// DESCRIPTION: Search the send check result from MySQL
	// EXAMPLE :  
	// RETURN: $result_array
	// ############################################################################ 
	function account_search_result($chk_username) {
		$db_select    = Sql_Account::sql_db;
		$db_table     = Sql_Account::sql_table;
		$table_name1  = Sql_Account::table_name1;
		$conn         = '';
		$sql_cmd      = '';
		$rows         = 0;
		$result       = '';
		$result_array = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// SELECT * FROM `account_member` ORDER BY `username` DESC
		// SELECT *  FROM `account_member` WHERE `username` = 'max_yin' ORDER BY `username` DESC
		$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1` = '$chk_username' ORDER BY `$table_name1` ASC";
		$result  = mysqli_query($conn,$sql_cmd);// 透过 execute_query 对象实例完成查询
		if ( !$result ) { 
			mysqli_close($conn);				// 关闭资料库
			return 0;
		}
		
		$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
		if ( $rows == 0 ) { 
			mysqli_close($conn);				// 关闭资料库
			return 0;
		}
		
		$result_array = mysqli_fetch_array($result,MYSQLI_ASSOC);	// 取得指定人名在 MySQL Account 各栏位的资料
		if ( !is_array($result_array) ) { 
			mysqli_free_result($result);		// 释放 MySQL $result
			mysqli_close($conn);				// 关闭资料库
			return 0;
		}
		mysqli_free_result($result);			// 释放 MySQL $result
		mysqli_close($conn);					// 关闭资料库
		return $result_array;
	}
	
	// ############################################################################
	// PROCEDURE NAME - account_delete($chk_username)
	// -------------------------------------------------------------------------
	// DESCRIPTION: To delete account data from MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function account_delete($chk_username) {
		$db_select    = Sql_Account::sql_db;
		$db_table     = Sql_Account::sql_table;
		$table_name1  = Sql_Account::table_name1;
		$conn         = '';
		$sql_cmd      = '';
		$rows         = 0;
		$result       = '';
		$result_array = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// DELETE FROM `account_member` WHERE `username` = 'max_yin'
		$sql_cmd = "DELETE FROM `$db_table` WHERE `$table_name1` = '$chk_username'";
		$result  = mysqli_query($conn,$sql_cmd);// 透过 execute_query 对象实例完成查询
		if ( !$result ) { 
			mysqli_close($conn);				// 关闭资料库
			return 0;
		}
		mysqli_close($conn);					// 关闭资料库
		return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - download_create($pn,$wsid,$tpver,$work_order,$line,$panel,$ftpip,$date_time,$creator,$discrption)
	// -------------------------------------------------------------------------
	// DESCRIPTION: Create new program data to MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function download_create($pn,$wsid,$tpver,$work_order,$line,$panel,$ftpip,$date_time,$creator,$discrption) {
		$db_select    = Sql_Download::sql_db;		// 'web_download';
		$db_table     = Sql_Download::sql_table;	// 'download_data2';
		$table_name1  = Sql_Download::table_name1;	// 'webdl_pn';
		$table_name2  = Sql_Download::table_name2;	// 'webdl_wsid';
		$table_name3  = Sql_Download::table_name3;	// 'webdl_tpver';
		$table_name4  = Sql_Download::table_name4;	// 'webdl_work_order';
		$table_name5  = Sql_Download::table_name5;	// 'webdl_line';
		$table_name6  = Sql_Download::table_name6;	// 'webdl_panel';
		$table_name7  = Sql_Download::table_name7;	// 'webdl_ftpip';
		$table_name8  = Sql_Download::table_name8;	// 'webdl_create_date';
		$table_name9  = Sql_Download::table_name9;	// 'webdl_creator';
		$table_name10 = Sql_Download::table_name10;	// 'webdl_description';
		$conn         = '';
		$result       = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// INSERT INTO `web_account`.`account_member` (`username`, `password`, `create_date`, `create_by`) VALUES ('test4', 'test4', '2015-06-14 18:29:10', 'max_yin');
		$sql_cmd = "INSERT INTO `$db_select`.`$db_table`(`$table_name1`,`$table_name2`,`$table_name3`,`$table_name4`,`$table_name5`,`$table_name6`,`$table_name7`,`$table_name8`,`$table_name9`,`$table_name10`) VALUES('$pn','$wsid','$tpver','$work_order','$line','$panel','$ftpip','$date_time','$creator','$discrption')";
		$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成添加
		mysqli_close($conn);						// 关闭资料库
		if ( !$result ) return 0; else return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - download_update($pn,$wsid,$tpver,$work_order,$line,$panel,$ftpip,$date_time,$creator,$discrption)
	// -------------------------------------------------------------------------
	// DESCRIPTION: To update download data to MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function download_update($pn,$wsid,$tpver,$work_order,$line,$panel,$ftpip,$date_time,$creator,$discrption) {
		$db_select    = Sql_Download::sql_db;
		$db_table     = Sql_Download::sql_table;
		$table_name1  = Sql_Download::table_name1;	// 'webdl_pn';
		$table_name2  = Sql_Download::table_name2;	// 'webdl_wsid';
		$table_name3  = Sql_Download::table_name3;	// 'webdl_tpver';
		$table_name4  = Sql_Download::table_name4;	// 'webdl_work_order';
		$table_name5  = Sql_Download::table_name5;	// 'webdl_line';
		$table_name6  = Sql_Download::table_name6;	// 'webdl_panel';
		$table_name7  = Sql_Download::table_name7;	// 'webdl_ftpip';
		$table_name8  = Sql_Download::table_name8;	// 'webdl_create_date';
		$table_name9  = Sql_Download::table_name9;	// 'webdl_creator';
		$table_name10 = Sql_Download::table_name10;	// 'webdl_description';
		$conn         = '';
		$rows         = 0;
		$result       = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// UPDATE `account_member` SET `username`=[value-1],`password`=[value-2],`create_date`=[value-3],`create_by`=[value-4] WHERE `username` = 'max_yin'
		$sql_cmd = "UPDATE `$db_table` SET `$table_name1`='$pn',`$table_name2`='$wsid',`$table_name3`='$tpver',`$table_name4`='$work_order',`$table_name5`='$line',`$table_name6`='$panel',`$table_name7`='$ftpip',`$table_name8`='$date_time',`$table_name9`='$creator',`$table_name10`='$discrption' WHERE `$table_name1`='$pn' AND `$table_name2`='$wsid' AND `$table_name3`='$tpver' AND `$table_name4`='$work_order' AND `$table_name5`='$line' AND `$table_name6`='$panel'";
		$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成添加
		$rows    = mysqli_affected_rows($conn);		// 获取刚刚更新资料笔数
		mysqli_close($conn);						// 关闭资料库
		if ( $rows == 0 ) return 0;
		if ( !$result ) return 0; else return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - download_delete($pn,$wsid,$tpver,$work_order,$line,$panel) {
	// -------------------------------------------------------------------------
	// DESCRIPTION: To delete download data from MySQL
	// EXAMPLE :  
	// RETURN: 0/1
	// ############################################################################ 
	function download_delete($pn,$wsid,$tpver,$work_order,$line,$panel) {
		$db_select    = Sql_Download::sql_db;
		$db_table     = Sql_Download::sql_table;
		$table_name1  = Sql_Download::table_name1;	// 'webdl_pn';
		$table_name2  = Sql_Download::table_name2;	// 'webdl_wsid';
		$table_name3  = Sql_Download::table_name3;	// 'webdl_tpver';
		$table_name4  = Sql_Download::table_name4;	// 'webdl_work_order';
		$table_name5  = Sql_Download::table_name5;	// 'webdl_line';
		$table_name6  = Sql_Download::table_name6;	// 'webdl_panel';
		$conn         = '';
		$sql_cmd      = '';
		$rows         = 0;
		$result       = '';
		$result_array = '';
		
		// Check connection
		$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
		if ( mysqli_connect_errno($conn) ) { 
			echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
		}
		mysqli_set_charset($conn,'set names utf8');
		
		// 要是字段是字符型，必须要用''（单引号）括起来，要是数值型，若中间有空格，可以用''（单引号）括起来; 若没有空格，也可以不用...
		// DELETE FROM `download_data` WHERE `pn` = 'CRL615766000A' AND `wsid` = 'webul_ftb1' AND `tpver` = '0X1.16'
		$sql_cmd = "DELETE FROM `$db_table` WHERE `$table_name1`='$pn' AND `$table_name2`='$wsid' AND `$table_name3`='$tpver' AND `$table_name4`='$work_order' AND `$table_name5`='$line' AND `$table_name6`='$panel'";
		$result  = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
		if ( !$result ) { 
			mysqli_close($conn);					// 关闭资料库
			return 0;
		}
		mysqli_close($conn);						// 关闭资料库
		return 1;
	}
	
	// ############################################################################
	// PROCEDURE NAME - AnalyzeMySqlStr($data,$checkStr)
	// -------------------------------------------------------------------------
	// DESCRIPTION: To Analyze Key Word from MySQL data
	// EXAMPLE :  
	// RETURN: $data[$checkStr]
	// ############################################################################ 
	function AnalyzeMySqlStr($data,$checkStr) {
		return $data[$checkStr];
	}
	
	// ############################################################################
	// PROCEDURE NAME - GetFtpPath($wsid,$tpver,$pn,$line,$work_order,$panel)
	// -------------------------------------------------------------------------
	// DESCRIPTION: To Analyze ftp upload path
	// EXAMPLE :  
	// RETURN: $put_ftp_path
	// ############################################################################ 
	function GetFtpPath($wsid,$tpver,$pn,$line,$work_order,$panel) {
		// each_ftpip = ftp://ICT:!xiuqun123@10.10.30.65/ICT/[path]/
		$web_user  	= html::ftp_upload_user;		// FTP Web update user name
		$web_pass  	= html::ftp_upload_pass;		// FTP Web update password
		$web_ftp_ip	= html::FtpIP;					// '10.10.6.5';
		$new_wsid	= '';
		
		// ex: F/T-BI2C_SOP -> FT-BI2C_SOP
		if ( strpos($wsid,',') > -1 ) {
			$new_wsid = "COMMON";
		} else {
			$get_wsid_array = preg_split("/[\/]+/",$wsid);
			foreach ( $get_wsid_array as $eachStr ) {
				if ( $eachStr == NULL ) continue;
				$new_wsid = sprintf("%s%s",$new_wsid,$eachStr);
			}
		}
		
		if ( strpos($wsid,'H/P') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/hitpot/%s/%s/",$web_user,$web_pass,$web_ftp_ip,$pn,$new_wsid);
		} elseif ( strpos($wsid,'FLASH') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/flash/%s/%s/",$web_user,$web_pass,$web_ftp_ip,$pn,$work_order);
		} elseif ( strpos($wsid,'SCT') > -1 || strpos($wsid,"ICT") > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/ict/%s/%s/",$web_user,$web_pass,$web_ftp_ip,$new_wsid,$pn);
		} elseif ( strpos($wsid,'DOC') > -1 ) {
			if ( strpos($tpver,'DOC_') > -1 ) {
				$starStr        = "DOC_";
				$start          = strpos($tpver,$starStr)+strlen($starStr);
				$GetDocClass    = strtoupper( substr($tpver,$start,3) );
				$put_ftp_path   = sprintf("ftp://%s:%s@%s/document/%s/",$web_user,$web_pass,$web_ftp_ip,$GetDocClass);
			} else {
				$put_ftp_path   = sprintf("ftp://%s:%s@%s/document/COMMON/",$web_user,$web_pass,$web_ftp_ip);
			}
		} elseif ( strpos($wsid,'SMT') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/smt/%s/%s/%s/",$web_user,$web_pass,$web_ftp_ip,$pn,$line,$panel);
		} elseif ( strpos($wsid,'ROUTER') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/router/",$web_user,$web_pass,$web_ftp_ip);
		} elseif ( strpos($wsid,'WS-PROFILE') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/wsprofile/",$web_user,$web_pass,$web_ftp_ip);
		} elseif ( strpos($wsid,'ESD') > -1 ) {
			$put_ftp_path = sprintf("ftp://%s:%s@%s/esd/",$web_user,$web_pass,$web_ftp_ip);
		} else {
			// For MFG path
			$put_ftp_path = sprintf("ftp://%s:%s@%s/mfg/%s/%s/%s/",$web_user,$web_pass,$web_ftp_ip,$pn,$work_order,$new_wsid);
		}
		return $put_ftp_path;
	}
?>


